package org.lojzes.minstruts.reflect;

import java.lang.reflect.Field;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lojzes.minstruts.action.Action;
import org.lojzes.minstruts.form.AbstractForm;

public class ClassFull {
	private static Log log = LogFactory.getLog(ClassFull.class);
	private static ThreadLocal<ClassFull> localData = new ThreadLocal<ClassFull>();
	private static ClassFull instance = null;

	private ClassFull() {
	}

	public static ClassFull getInstance() {

		if (null == localData.get()) {
			instance = new ClassFull();
			localData.set(instance);
		}
		return localData.get();
	}

	public AbstractForm fullForm(String formClass, HttpServletRequest request) {
		AbstractForm absForm = null;
		try {
			@SuppressWarnings("rawtypes")
			Class classz = Class.forName(formClass);
			absForm = (AbstractForm) classz.newInstance();
			Field[] fileds = classz.getDeclaredFields();
			for (Field field : fileds) {
				field.setAccessible(true);
				field.set(absForm, request.getParameter(field.getName()));
				field.setAccessible(false);
			}
		} catch (Exception e) {
			log.error("===>>严重：" + e);
		}
		return absForm;
	}
	public Action fullAction(String actionType){
		   Action action = null;
		   try {
			@SuppressWarnings("rawtypes")
			Class classz = Class.forName(actionType);
		    action = (Action)classz.newInstance();
		} catch (Exception e) {
			log.error("===>>严重："+e);
		}
		   return action;
	   }
}
